package com.sunlake.spring.main.common.model.file.controller;

import com.sunlake.spring.main.common.model.file.dto.FileUploadChunkDTO;
import com.sunlake.spring.main.common.model.file.service.impl.ResumeUploadServiceImpl;
import com.sunlake.spring.main.common.response.R;
import com.sunlake.spring.main.common.model.file.dto.FileMergeDTO;
import com.sunlake.spring.main.common.model.file.service.FileUploadService;
import com.sunlake.spring.main.common.utils.LoggerUtil;
import com.sunlake.spring.main.model.counselor.controller.CounselorApplyController;
import com.sunlake.spring.main.model.counselor.dto.ListRecentDTO;
import com.sunlake.spring.main.model.counselor.dto.ResumeMergeDTO;
import com.sunlake.spring.main.model.counselor.service.CounselorApplyService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * 文件上传控制器
 * 提供通用文件上传、分片上传、断点续传和文件合并功能
 * 不依赖第三方库实现
 */
@RestController
@RequestMapping("/api/file/upload")
public class FileUploadController {

    private static final Logger log = LoggerUtil.getLogger(CounselorApplyController.class);

    @Autowired
    private ResumeUploadServiceImpl resumeUploadService;

//    /**
//     * 上传简历文件
//     */
//    @PostMapping("/upload-resume")
//    public R<Map<String, Object>> uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
//        return counselorApplyService.uploadFile(file);
//    }

    /**
     * 分片上传简历
     */
    @PostMapping("/chunk")
    public R<Map<String, Object>> uploadFileChunk(
            @RequestParam("fileId") String fileId,
            @RequestParam("chunkIndex") Integer chunkIndex,
            @RequestParam("module") String module,
            @RequestPart("chunk") MultipartFile chunk
    ) throws IOException {
        // 将前端参数映射到后端参数
        // 使用fileId作为md5，chunkIndex作为index
        System.out.println(module);
        if(module.equals("resume")) {
            return resumeUploadService.uploadFileChunk(chunk, fileId, chunkIndex);
        }
        return R.error(500,"module not supported");
    }

    /**
     * 合并分片文件
     */
    @PostMapping("/merge")
    public R<Map<String, Object>> mergeFile(@RequestBody FileMergeDTO mergeDTO) throws IOException {
        // 直接传递，后续在DTO中添加fileId字段
        return resumeUploadService.mergeFile(mergeDTO);
    }

}